<%@page import="cs236369.hw5.RegistrationServiceStub"%>
<%@page import="cs236369.hw5.RegistrationServiceStub.GetRegisteredEndpointsResponse"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
<title>Search Yellow Pages</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var ws_functions = new Array();

function addWS(function_name) {
	ws_functions.push(function_name);
	
}


function searchAllWS() {
	for (var i = 0; i < ws_functions.length; i++) {
		ws_functions[i]();
	}
}



</script>

</head>
<body onload = "setPage('yellow_pages')">
<%@ include file="WEB-INF/private/body_wrapper_header.html" %>

<h1>All available Web Services on Yellow Pages</h1>

<ul id="search_list">
<%
RegistrationServiceStub stub = new RegistrationServiceStub();

GetRegisteredEndpointsResponse registeredEndpointsResponse = stub.getRegisteredEndpoints();

String[] endpointsArray = registeredEndpointsResponse.get_return();

for (int i = 0; i < endpointsArray.length; i++) { %>
	<li class="service_provider"><%=  endpointsArray[i] %></li>
	<ul id="<%=endpointsArray[i] %>_results" ></ul>
	<script type="text/javascript">
	function <%=endpointsArray[i]%>_search() {
		var keywords = document.getElementById("keywords").value;
		<%=endpointsArray[i]%>_request = new XMLHttpRequest();	
		<%=endpointsArray[i]%>_request.open("GET", "SearchOnWS?ws_name=<%=endpointsArray[i]%>&keywords=" + keywords, true);
		<%=endpointsArray[i]%>_request.onreadystatechange = <%=endpointsArray[i]%>_ready;
		<%=endpointsArray[i]%>_request.send(null);
		
	}
	
	
	function <%=endpointsArray[i]%>_ready() {
		var resList = document.getElementById("<%=endpointsArray[i] %>_results");

		if(<%=endpointsArray[i]%>_request.readyState < 4)
		{
			resList.innerHTML = "<li>Loading...</li>";
			return;
		}
		else if(<%=endpointsArray[i]%>_request.readyState == 4 && <%=endpointsArray[i]%>_request.status == 200)
		{
			resList.innerHTML = "";
			
			var response = <%=endpointsArray[i]%>_request.responseText;
			var respElements = response.split("<ENDOFCOURSE>");
			
			for (var i = 0; i < respElements.length - 1; i++) {
				resList.innerHTML += "<li>" + respElements[i] + "</li>";
			}
		}
		else {
			resList.innerHTML = "<li>WS returned Error</li>";
	    }
	
	}
		
		
	
	
	
	
	
	
	
	addWS(<%=endpointsArray[i]%>_search);
	
	
	</script>
<% } %>
</ul>

<h4>Search in all WS. Enter keyword with spaces between them</h4>

<input id="keywords" type="text"></input>
<input type="submit" value="Search" onclick="searchAllWS()"></input>

	





<%@ include file="WEB-INF/private/body_wrapper_footer.html" %>
</body>
</html>